package se.webbzon.boltzmann.editor.factory.propertyframe;

import java.util.Collection;

import se.webbzon.boltzmann.editor.component.appearance.SimpleAppearancePanel;
import se.webbzon.boltzmann.editor.component.dependencylist.DependencyComboBox;
import se.webbzon.boltzmann.editor.mvc.EditorModel;
import se.webbzon.boltzmann.editor.propertyframe.WorldObjectPropertyFrame;
import se.webbzon.boltzmann.editor.propertyframe.WorldStaticObjectPropertyFrame;
import se.webbzon.boltzmann.game.object.waypoint.Waypoint;
import se.webbzon.oschi01.worldobject.WorldObject;
import se.webbzon.oschi01.worldobject.appearance.SimpleAppearance3D;

public class WaypointPropertyFrameFactory implements PropertyFrameFactory {

	/*============================================================
	Public Methods
	============================================================*/
	
	@Override public String getWorldObjectName() {
		return Waypoint.worldObjectName;
	}

	@Override public WorldObjectPropertyFrame makePropertyFrame(EditorModel model,
																WorldObject instance) {
		
		return new PropertyFrameImpl(model, (Waypoint) instance);
	}
	
	/*============================================================
	Private Classes
	============================================================*/
	
	private static final class PropertyFrameImpl extends WorldStaticObjectPropertyFrame {
		
		// The appearance panel of this property frame
		private final SimpleAppearancePanel appearancePanel;
		
		// The dependency combo box input of this property frame
		private final DependencyComboBox<Waypoint> next;
		
		/*============================================================
		Constructors
		============================================================*/
		
		public PropertyFrameImpl(EditorModel model, Waypoint instance) {
			super(model, instance);
			
			
			// Create appearance panel
			SimpleAppearance3D appearance = 
					(SimpleAppearance3D) instance.getAppearance();
			appearancePanel = new SimpleAppearancePanel(appearance);
			addPanel(appearancePanel.getName(),appearancePanel);
			
			// Create dependency panel
			next = new DependencyComboBox<Waypoint>(model,Waypoint.class,
													((Waypoint) instance).getNext(),
													"Next Waypoint: ");
			
			addPanel(next.getName(),next);	
			initialize();
		}
		
		/*============================================================
		Public Methods
		============================================================*/
		
		@Override public boolean apply(Collection<String> errorMessages) {
			if (super.apply(errorMessages)) try {
				Waypoint waypoint = (Waypoint) getInstance();
				SimpleAppearance3D appearance = 
						(SimpleAppearance3D) waypoint.getAppearance();
				appearance.setColor(appearancePanel.getColor());
				waypoint.setNext(next.getInstance());
				return true;
			} catch (ClassCastException e) {
				errorMessages.add(e.getMessage());
				return false;
			} else
				return false;
		}
		
	}

}
